<?php
/**
 *
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/25
 * Time: 11:32
 */
namespace app\admin\controller;
use app\data\Data;
use app\validate\GroupValidate;
use think\Config;
use think\Request;

/**
 * +-----------------------------------
 * Class Group
 * @package app\admin\controller
 * 角色分组列表
 * +------------------------------------
 */
class Group extends Check{
    /**
     * @return \think\response\View
     * 角色列表
     */
    public function groupList(){
        $group = new Data('auth_group');
        $list = $group->selectData();
        return view('groupList',['list'=>$list]);
    }

    /**
     * @return \think\response\View
     * 添加角色分组
     */
    public function groupAdd(){
        try{
            $rule = new Data('auth_rule');
            if(Request::instance()->isPost()){
                $group = new Data('auth_group');
                $post = Request::instance()->post();
                if(empty($post['ids'])){
                    throw new \LogicException(Config::get('error_table.rule')['msg'],Config::get('error_table.rule')['code']);
                }
                $post['rules'] = implode(',',$post['ids']);
                unset($post['ids']);
                $group_validate = new GroupValidate();
                $validate_rule  =  $group_validate->groupValidate();
                self::validateData($validate_rule['rule'], $validate_rule['message'],$post);       /*验证数据*/
                $group_bool = $group->insertData($post);
                if(!$group_bool){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        $list = $rule->selectData("p_id = 0");
        foreach($list as $v){
            $result[$v['title']] = $rule->selectData("p_id = {$v['id']}");
        }
        return view('groupAdd',['list'=>$result]);
    }

    /**
     * @return array|\think\response\View
     * 更新角色分组
     */
    public function groupSave(){
        try{
            $rule = new Data('auth_rule');
            $group = new Data('auth_group');
            if(Request::instance()->isPost()){
                $post = Request::instance()->post();
                if(empty($post['ids'])){
                    throw new \LogicException(Config::get('error_table.rule')['msg'],Config::get('error_table.rule')['code']);
                }
                $post['rules'] = implode(',',$post['ids']);
                unset($post['ids']);
                $group_validate = new GroupValidate();
                $validate_rule  =  $group_validate->groupValidate();
                self::validateData($validate_rule['rule'], $validate_rule['message'],$post);       /*验证数据*/
                $group_save_bool = $group->updateData($post);
                if($group_save_bool === false){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        $id = intval(input('id'));
        $getOne = $group->find(['id'=>$id]);
        $list = $rule->selectData("p_id = 0");
        foreach($list as $v){
            $result[$v['title']] = $rule->selectData("p_id = {$v['id']}");
        }
        return view('groupSave',['list'=>$result,'one'=>$getOne]);
    }
}